//! 抗量子攻击加密算法
//!
//!

use crate::error::{Error, ErrorKind};
use ntrumls::{PrivateKey, PublicKey};

/// 产生秘钥对
pub fn generate_key_pair() -> Result<(PrivateKey, PublicKey), Error> {
    ntrumls::generate_keys(&ntrumls::params::XXX_20151024_907).ok_or(Error::from_kind(
        ErrorKind::Msg("generate key pair failed".to_string()),
    ))
}

/// 签名数据  用公钥和私钥共同加签
pub fn sign(private_key: &[u8], public_key: &[u8], msg: &[u8]) -> Result<Vec<u8>, Error> {
    let private = PrivateKey::import(private_key);
    let public = PublicKey::import(public_key);

    Ok(ntrumls::sign(&private, &public, msg)
        .ok_or(Error::from_kind(ErrorKind::Msg(
            "
                    ntru sign failed
                    "
            .to_string(),
        )))?
        .to_vec())
}

/// 验签
pub fn verify(data: &[u8], signature: &[u8], public_key: &[u8]) -> bool {
    let public = PublicKey::import(public_key);
    ntrumls::verify(signature, &public, data)
}
